package com.china08.yxyapi.api.manage.tec.teaching.scoreport.baseanalysis;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.math3.stat.descriptive.moment.Skewness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResWeikeKnow;
import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.ExamStudent;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamBaseAnalyse;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamQuesAnalyse;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResWeikeKnowRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamBaseAnalyseRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamQuesAnalyseRepsository;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamStudentRepository;
import com.china08.yxyapi.util.algorithm.ReportAlgorithm;

/**
 * 创建考试 基础分析 成绩环比
 * 
 */
@CrossOrigin
@RestController
public class BaseAnalysisLinkRatioController extends YxyApiBaseController {
    @Autowired
    private ExamRepository examRepository;
    @Autowired
    private ExamStudentRepository examStudentRepository;
    @Autowired
    private ExamBaseAnalyseRepository examBaseAnalyseRepository;
    @Autowired
    private ExamQuesAnalyseRepsository examQuesAnalyseRepsository;
    @Autowired
    private YxyResWeikeKnowRepository yxyResWeikeKnowRepository;
    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/baseanalysis/linkratio", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel){
        RespModel model = new RespModel();
        Integer isLast=0; //标记是否有上次考试
        Exam exam = new Exam();
        if (examRepository.findById(reqModel.examId) != null) {
            exam = examRepository.findById(reqModel.examId);
        } // 本次考试实体类
          // Exam exam = examRepository.findById(reqModel.examId);// 本次考试实体类
        String teacherId = getProfile(request, response).getId(); // 发布用户:ID
        List<Exam> examLst = new ArrayList<Exam>();
        if (exam.getId() != null) {
            examLst = examRepository.findByTeacherIdAndClassIdAndSubjectAndStatusNotOrderByExamDateDescCreatedDateDesc(teacherId,
                    exam.getClassId(),exam.getSubject(),0);
        }
        Integer index = -1;// 下标值
        Date datea = null;
        Date dateb = null;
        Integer xiabiao = 0;
        if (examLst.size() <= 1) {
//            Assert.isTrue(examLst.size() >= 2, "本次考试是第一次考试,没有上次考试!");
        	isLast =1;
            return model;
        }
        for (Exam exam2 : examLst) {
            index++;
            datea = exam2.getCreatedDate();
            dateb = exam.getCreatedDate();
            if (datea == null || dateb == null) {
                continue;
            } else if (datea.compareTo(dateb) == 0) { // 如果当前的日期和本次考试的日期相同,则获取下标值
                xiabiao = index + 1;
                break;
            }
        }
        Exam examLast = null;
        if (examLst.size() - 1 < xiabiao) {
        	isLast =1;
//            Assert.isTrue(examLst.size() - 1 >= xiabiao, "本次考试是第一次考试,没有上次考试!");
            return model;
        } else if (examLst.size() - 1 >= xiabiao && xiabiao >= 0) {
            examLast = examLst.get(xiabiao); // 上次考试实体类
        }

        // ------考试信息-----//
        ExamBenCi examBenCi = new ExamBenCi();
        if (exam != null && exam.getName() != null) {
            examBenCi.examName = exam.getName(); // 本次考试名称
        } else {
            examBenCi.examName = ""; // 本次考试名称
        }
        examBenCi.totalScore = exam.getTotalScore();// 本次考试总分
        
        
        
        
        
        if (exam != null && exam.getQuestions() != null) {
            examBenCi.questionCount = exam.getQuestions().size();// 本次考试的数量
        } else {
            examBenCi.questionCount = 0;// 本次考试的数量
        }
        List<String> charpterList = new ArrayList<String>();
        if (exam != null && exam.getBookContents() != null) {
            for (Exam.BookContent exam2 : exam.getBookContents()) {
                charpterList.add(exam2.name); // 章节名称
            }
        }
        examBenCi.charpterList = charpterList;
        DecimalFormat df = new DecimalFormat("######0.00");
        // 本次考试涉及到的学生
        exam.setId(reqModel.examId);
        Long studentCount = examStudentRepository.countByExamAndStuTotalScoreNot(exam, -1);
        examBenCi.personCount = studentCount;
        exam.setId(reqModel.examId);
        ExamBaseAnalyse examBaseAnalyse = examBaseAnalyseRepository.findByExam(exam);
        if (examBaseAnalyse != null) {
            examBenCi.average = examBaseAnalyse.getAverage() == null ? 0.0
                    : Double.parseDouble(df.format(examBaseAnalyse.getAverage()));// 平均分
            examBenCi.max = examBaseAnalyse.getMax() == null ? 0.0
                    : Double.parseDouble(df.format(examBaseAnalyse.getMax()));// 最高分
            examBenCi.min = examBaseAnalyse.getMin() == null ? 0.0
                    : Double.parseDouble(df.format(examBaseAnalyse.getMin()));// 最低分
            examBenCi.lostScoreRate = examBaseAnalyse.getLostScoreRate() == null ? 0
                    : (int) (Double.parseDouble(df.format(examBaseAnalyse.getLostScoreRate())));// 失分率
        }

        /*Map<Integer, Integer> fineRank = new HashMap<Integer, Integer>();
        if (examBaseAnalyse != null && examBaseAnalyse.getFineRank() != null) {
            fineRank = examBaseAnalyse.getFineRank();
        } */// 优良分布图 key: 5,4,3,2,1 分别对应 "优秀","良好","及格","不及格","不及格过差"
        Integer youxiu1 = 0;
        Integer bujige1 = 0;
        Integer jige = 0;
        Integer lianghao = 0;
        
        
        youxiu1 = (int) (examBenCi.totalScore *0.9);
        bujige1 = (int) (examBenCi.totalScore * 0.6);
        jige = (int) (examBenCi.totalScore * 0.6);
        lianghao = (int) (examBenCi.totalScore * 0.8);
       /* if (fineRank.size() > 0) {
            for (Integer key : fineRank.keySet()) { // key: 5,4,3,2,1 分别对应
                                                    // "优秀","良好","及格","不及格","不及格过差"</br>
                if (key == 5) {
                    youxiu1 = fineRank.get(key);
                }
                if (key == 2) {
                    bujige1 = fineRank.get(key);
                }
                if (key == 3) {
                    jige = fineRank.get(key);
                }
                if (key == 4) {
                    lianghao = fineRank.get(key);
                }
            }
        }*/
        
        
        // 不及格分数
        examBenCi.bujigeScore = bujige1;
        // 及格分数
        examBenCi.jigeScore = jige;
        // 良好分数
        examBenCi.lianghaoScore = lianghao;
        // 优秀分数
        examBenCi.youxiuScore = youxiu1;

        // 优秀的人数
        Long youxiuCount = examStudentRepository.countByExamAndStuTotalScoreGreaterThanEqual(exam, youxiu1);
        Double you = 0.0;
        if (studentCount != 0) {
            you = ((double) youxiuCount / studentCount);
        }
        // 良好的人数
        Long lianghaoCount = examStudentRepository.countByExamAndStuTotalScoreBetween(exam, lianghao-0.1, youxiu1);
        Double liang = 0.0;
        if (studentCount != 0) {
            liang = ((double) lianghaoCount / studentCount);
        }
        // 及格的人数
        Long jigeCount = examStudentRepository.countByExamAndStuTotalScoreBetween(exam, jige-0.1, lianghao);
        Double jigeBai = 0.0;
        if (studentCount != 0) {
            jigeBai = ((double) jigeCount / studentCount);
        }
        // 不及格的人数
        Long bujigeCount = examStudentRepository.countByExamAndStuTotalScoreLessThan(exam, bujige1);
        Double bujige = 0.0;
        if (bujigeCount != 0) {
            bujige = ((double) bujigeCount / studentCount);
        }

        examBenCi.bujigeCount = bujigeCount;
        examBenCi.jigeCount = jigeCount;
        examBenCi.lianghaoCount = lianghaoCount;
        examBenCi.youxiuCount = youxiuCount;

        // 不及格人数所占的百分比
        examBenCi.bujigePerson = Double.parseDouble(df.format(bujige));
        // 及格人数所占的百分比
        examBenCi.jigePerson = Double.parseDouble(df.format(jigeBai));
        // 良好人数所占的百分比
        examBenCi.lianghaoPerson = Double.parseDouble(df.format(liang));
        // 优秀人数所占的百分比
        examBenCi.youxiuPerson = Double.parseDouble(df.format(you));

        // 错误最多的知识点
        ExamQuesAnalyse examQuesAnalyse = examQuesAnalyseRepsository.findTopByExamOrderByRightRateAsc(exam);
        List<String> knowledgeList = new ArrayList<String>();
        if (examQuesAnalyse != null && examQuesAnalyse.getBookContents() != null) {
            knowledgeList = examQuesAnalyse.getKnows();
        }
        List<String> knowledgeListLast3 = new ArrayList<String>();
        Object yxyResWeikeKnow1=null;
        for (String knowledgeid : knowledgeList) {
        	yxyResWeikeKnow1 = yxyResWeikeKnowRepository.findDistinctYxyResWeikeKnowByKnowId(Integer.valueOf(knowledgeid));
        	if (yxyResWeikeKnow1 != null && !yxyResWeikeKnow1.equals("")) {				
        		knowledgeListLast3.add(yxyResWeikeKnow1.toString());
			}
		}
        examBenCi.knowledgeList = knowledgeListLast3;

        Double bujigeS = 0.0;
        Double jigeS = 0.0;
        Double lianghaoS = 0.0;
        Double youxiuS = 0.0;
        // ----集中段----//
        if (studentCount > 0) {
            bujigeS = ((double) bujigeCount / studentCount);
            jigeS = ((double) jigeCount / studentCount);
            lianghaoS = ((double) lianghaoCount / studentCount);
            youxiuS = ((double) youxiuCount / studentCount);
        }

        List<ScoreFenBu> scoreFenBuLst = new ArrayList<ScoreFenBu>();

        ScoreFenBu scoreFenBu2 = new ScoreFenBu();
        scoreFenBu2.key = "不及格";
        scoreFenBu2.value = bujigeS;
        scoreFenBuLst.add(scoreFenBu2);
        ScoreFenBu scoreFenBu3 = new ScoreFenBu();
        scoreFenBu3.key = "及格";
        scoreFenBu3.value = jigeS;
        scoreFenBuLst.add(scoreFenBu3);
        ScoreFenBu scoreFenBu4 = new ScoreFenBu();
        scoreFenBu4.key = "良好";
        scoreFenBu4.value = lianghaoS;
        scoreFenBuLst.add(scoreFenBu4);
        ScoreFenBu scoreFenBu5 = new ScoreFenBu();
        scoreFenBu5.key = "优秀";
        scoreFenBu5.value = youxiuS;
        scoreFenBuLst.add(scoreFenBu5);

        /*
         * Collections.sort(scoreFenBuLst, new Comparator<ScoreFenBu>() {
         * 
         * @Override public double compare(ScoreFenBu o1, ScoreFenBu o2) {
         * return o2.value - o1.value; } });
         */
        int i, j;
        for (i = 0; i < scoreFenBuLst.size(); i++) {
            for (j = i + 1; j < scoreFenBuLst.size(); j++) {
                if (scoreFenBuLst.get(i).value < scoreFenBuLst.get(j).value) {
                    ScoreFenBu scoreFenBu = null;
                    scoreFenBu = scoreFenBuLst.get(i);
                    scoreFenBuLst.set(i, scoreFenBuLst.get(j));
                    scoreFenBuLst.set(j, scoreFenBu);
                }
            }
        }
        // ---上次考试-----//
        ExamLast examLastModel = new ExamLast();
        if (examLast != null) {
            examLastModel.examName = examLast.getName() == null ? "" : examLast.getName(); // 本次考试名称
            examLastModel.questionCount = examLast.getQuestions() == null ? 0 : examLast.getQuestions().size();// 本次考试的数量
            examLastModel.totalScore = examLast.getTotalScore();
        } else {
            examLastModel.examName = "";
            examLastModel.questionCount = 0;

            examLastModel.totalScore = 0;
        }

        List<String> charpterListLast = new ArrayList<String>();
        if (examLast != null && examLast.getBookContents() != null) {
            for (Exam.BookContent exam2 : examLast.getBookContents()) {
                charpterListLast.add(exam2.name); // 章节名称
            }
        }
        examLastModel.charpterList = charpterListLast;

        // 本次考试涉及到的学生
        Long studentCountLast = 0l;
        if (examLast != null) {
            studentCountLast = examStudentRepository.countByExamAndStuTotalScoreNot(examLast, -1);
        }
        examLastModel.personCount = studentCountLast;
        ExamBaseAnalyse examBaseAnalyseLast = examBaseAnalyseRepository.findByExam(examLast);
        if (examBaseAnalyseLast != null) {
            examLastModel.average = examBaseAnalyseLast.getAverage() == null ? 0.0
                    : Double.parseDouble(df.format(examBaseAnalyseLast.getAverage()));// 平均分
            examLastModel.max = examBaseAnalyseLast.getMax() == null ? 0.0
                    : Double.parseDouble(df.format(examBaseAnalyseLast.getMax()));// 最高分
            examLastModel.min = examBaseAnalyseLast.getMin() == null ? 0.0
                    : Double.parseDouble(df.format(examBaseAnalyseLast.getMin()));// 最低分
            examLastModel.lostScoreRate = examBaseAnalyseLast.getLostScoreRate() == null ? 0
                    : (int) (Double.parseDouble(df.format(examBaseAnalyseLast.getLostScoreRate())));// 失分率
        }

//        Map<Integer, Integer> fineRankLast = new HashMap<Integer, Integer>(); // 优良分布图
                                                                              // key:
                                                                              // 5,4,3,2,1
                                                                              // 分别对应
                                                                              // "优秀","良好","及格","不及格","不及格过差"
        /*if (examBaseAnalyseLast != null && examBaseAnalyseLast.getFineRank() != null) {
            fineRankLast = examBaseAnalyseLast.getFineRank();
        }*/
        Integer youxiu2 = 0;
        Integer bujige2 = 0;
        Integer jige2 = 0;
        Integer lianghao2 = 0;
        youxiu2 = (int) (examLastModel.totalScore *0.9);
        bujige2 = (int) (examLastModel.totalScore * 0.6);
        jige2 = (int) (examLastModel.totalScore * 0.6);
        lianghao2 = (int) (examLastModel.totalScore * 0.8);
        /*if (fineRankLast.size() > 0) {
            for (Integer key : fineRankLast.keySet()) { // key: 5,4,3,2,1 分别对应
                                                        // "优秀","良好","及格","不及格","不及格过差"</br>
                if (key == 5) {
                    youxiu2 = fineRank.get(key);
                }
                if (key == 2) {
                    bujige2 = fineRank.get(key);
                }
                if (key == 3) {
                    jige2 = fineRank.get(key);
                }
                if (key == 4) {
                    lianghao2 = fineRank.get(key);
                }
            }
        }*/

        // 不及格分数
        examLastModel.bujigeScore = bujige2;
        // 及格分数
        examLastModel.jigeScore = jige2;
        // 良好分数
        examLastModel.lianghaoScore = lianghao2;
        // 优秀分数
        examLastModel.youxiuScore = youxiu2;

        // 优秀的人数
        youxiuCount = examStudentRepository.countByExamAndStuTotalScoreGreaterThanEqual(examLast, youxiu2);
        Double you2 = 0.0;
        if (youxiuCount != 0 && studentCountLast != 0) {
            you2 = ((double) youxiuCount / studentCountLast);
        }
        // 良好的人数
        lianghaoCount = examStudentRepository.countByExamAndStuTotalScoreBetween(examLast, lianghao2-0.1, youxiu2);
        Double liang1 = 0.0;
        if (lianghaoCount != 0 && studentCountLast != 0) {
            liang1 = ((double) lianghaoCount / studentCountLast);
        }
        // 及格的人数
        jigeCount = examStudentRepository.countByExamAndStuTotalScoreBetween(examLast, jige2-0.1, lianghao2);
        Double jigeBai2 = 0.0;
        if (jigeCount != 0 && studentCountLast != 0) {
            jigeBai2 = ((double) jigeCount / studentCountLast);
        }
        // 不及格的人数
        bujigeCount = examStudentRepository.countByExamAndStuTotalScoreLessThan(examLast, bujige2);
        Double bujigePerson = 0.0;
        if (bujigeCount != 0 && studentCountLast != 0) {
            bujigePerson = ((double) bujigeCount / studentCountLast);
        }
        examLastModel.bujigeCount = bujigeCount;
        examLastModel.jigeCount = jigeCount;
        examLastModel.lianghaoCount = lianghaoCount;
        examLastModel.youxiuCount = youxiuCount;
        // 不及格人数所占的百分比
        examLastModel.bujigePerson = Double.parseDouble(df.format(bujigePerson));
        // 及格人数所占的百分比
        examLastModel.jigePerson = Double.parseDouble(df.format(jigeBai2));
        // 良好人数所占的百分比
        examLastModel.lianghaoPerson = Double.parseDouble(df.format(liang1));
        // 优秀人数所占的百分比
        examLastModel.youxiuPerson = Double.parseDouble(df.format(you2));

        // 错误最多的知识点
        ExamQuesAnalyse examQuesAnalyseLast = examQuesAnalyseRepsository.findTopByExamOrderByRightRateAsc(examLast);
        List<String> knowledgeListLast = new ArrayList<String>();
        if (examQuesAnalyseLast != null && examQuesAnalyseLast.getBookContents() != null) {
            knowledgeListLast = examQuesAnalyseLast.getKnows();
        }
        List<String> knowledgeListLast2 = new ArrayList<String>();
        Object yxyResWeikeKnow=null;
        for (String knowledgeid : knowledgeListLast) {
        	yxyResWeikeKnow = yxyResWeikeKnowRepository.findDistinctYxyResWeikeKnowByKnowId(Integer.valueOf(knowledgeid));
        	if (yxyResWeikeKnow != null && !yxyResWeikeKnow.equals("")) {				
        		knowledgeListLast2.add(yxyResWeikeKnow.toString());
			}
		}
        examLastModel.knowledgeList = knowledgeListLast2;
        String smark3 = "与近期考试进行比较发现,";
        if (you > you2) {
            smark3 += "高分成绩占比有所提升,";
        } else if (you == you2) {
            smark3 += "高分成绩与上次持平,";
        } else if (you < you2) {
            smark3 += "高分成绩占比有所降低,";
        }

        if (jigeBai > jigeBai2) {
            smark3 += "及格率有所提高,";
        } else if (jigeBai == jigeBai2) {
            smark3 += "及格率无明显变化,";
        } else if (jigeBai < jigeBai2) {
            smark3 += "及格率有所降低,";
        }

        // 低分成绩占比
        List<ExamStudent> examStudentBuJiGeLast = examStudentRepository.findByExamAndStuTotalScoreLessThan(examLast,
                bujige2);
        List<Fluctuate> bujigeScoreLst = new ArrayList<Fluctuate>();
        List<ExamStudent> examStudentBuJiBenCi = examStudentRepository.findByExamAndStuTotalScoreLessThan(exam,
                bujige1);
        Fluctuate fluctuate3 = null;
        List<Double> d = new ArrayList<Double>();
        if (examStudentBuJiGeLast != null && examStudentBuJiBenCi != null) {
            for (ExamStudent examStudent : examStudentBuJiBenCi) {
                for (ExamStudent examStudentbuJiGe : examStudentBuJiGeLast) {
                    if (examStudentbuJiGe.getStudentId().equals(examStudent.getStudentId())) {
                        fluctuate3 = new Fluctuate();
                        fluctuate3.studentNick = examStudent.getStudentNick();
                        d.add(examStudentbuJiGe.getStuTotalScore());
                        d.add(examStudent.getStuTotalScore());
                        fluctuate3.bodongdu = ReportAlgorithm.volatilityValue(d);
                        bujigeScoreLst.add(fluctuate3);
                    }
                }
            }
        }

        Collections.sort(bujigeScoreLst, new Comparator<Fluctuate>() {
            @Override
            public int compare(Fluctuate o1, Fluctuate o2) {
                return (int) (o2.bodongdu - o1.bodongdu);
            }
        });
        String stuNick = "";
        if (bujigeScoreLst.size() > 3) {
            for (Fluctuate fluctuate : bujigeScoreLst.subList(0, 3)) {
                stuNick += fluctuate.studentNick + ",";
            }
            smark3 += "其中低分的" + stuNick.substring(0, stuNick.length() - 1) + "没有明显进步,";
        } else if (bujigeScoreLst.size() <= 3 && bujigeScoreLst.size() > 0) {
            for (Fluctuate fluctuate : bujigeScoreLst) {
                stuNick += fluctuate.studentNick + ",";
            }
            smark3 += "其中低分的" + stuNick.substring(0, stuNick.length() - 1) + "没有明显进步,";
        }
        Double bujigeL = 0.0;
        Double jigeL = 0.0;
        Double lianghaoL = 0.0;
        Double youxiuL = 0.0;
        // ----集中段----//
        if (studentCountLast != 0) {
            bujigeL = ((double) bujigeCount / studentCountLast);
            jigeL = ((double) jigeCount / studentCountLast);
            lianghaoL = ((double) lianghaoCount / studentCountLast);
            youxiuL = ((double) youxiuCount / studentCountLast);
        }

        List<ScoreFenBu> scoreFenBuL = new ArrayList<ScoreFenBu>();

        ScoreFenBu scoreFenBuL2 = new ScoreFenBu();
        scoreFenBuL2.key = "不及格";
        scoreFenBuL2.value = bujigeL;
        scoreFenBuL.add(scoreFenBuL2);
        ScoreFenBu scoreFenBuL3 = new ScoreFenBu();
        scoreFenBuL3.key = "及格";
        scoreFenBuL3.value = jigeL;
        scoreFenBuL.add(scoreFenBuL3);
        ScoreFenBu scoreFenBuL4 = new ScoreFenBu();
        scoreFenBuL4.key = "良好";
        scoreFenBuL4.value = lianghaoL;
        scoreFenBuL.add(scoreFenBuL4);
        ScoreFenBu scoreFenBuL5 = new ScoreFenBu();
        scoreFenBuL5.key = "优秀";
        scoreFenBuL5.value = youxiuL;
        scoreFenBuL.add(scoreFenBuL5);

        /*
         * Collections.sort(scoreFenBuL, new Comparator<ScoreFenBu>() {
         * 
         * @Override public int compare(ScoreFenBu o1, ScoreFenBu o2) { return
         * (int)(o2.value - o1.value); } });
         */

        for (i = 0; i < scoreFenBuL.size(); i++) {
            for (j = i + 1; j < scoreFenBuL.size(); j++) {
                if (scoreFenBuL.get(i).value < scoreFenBuL.get(j).value) {
                    ScoreFenBu scoreFenBu = null;
                    scoreFenBu = scoreFenBuL.get(i);
                    scoreFenBuL.set(i, scoreFenBuL.get(j));
                    scoreFenBuL.set(j, scoreFenBu);
                }
            }
        }
        if (scoreFenBuLst.get(0).key.equals(scoreFenBuL.get(0).key)) {
            smark3 += "两次考试分数都集中在" + scoreFenBuL.get(0).key + "之间.";
        } else {
            smark3 += "两次考试分数都集中在" + scoreFenBuLst.get(0).key + "," + scoreFenBuL.get(0).key + "之间.";
        }

        // 两次考试最低分名单
        ExamStudent examSt = examStudentRepository.findTopByExamOrderByStuTotalScoreAscCreatedDateAsc(exam);// 学生名称(本次)
        ExamStudent examSt2 = examStudentRepository.findTopByExamOrderByStuTotalScoreAscCreatedDateAsc(examLast);// 学生名称(上次)
        List<String> smark = new ArrayList<String>();
        if (examSt != null && examSt.getStudentNick() != null) {
            smark.add(examSt.getStudentNick());
        }
        if (examSt2 != null && examSt2.getStudentNick() != null) {
            smark.add(examSt2.getStudentNick());
        }

        // 进步最大的学生
        List<ExamStudent> s = new ArrayList<ExamStudent>();
        s = examStudentRepository.findByExamOrderByStuTotalScoreDescCreatedDateAsc(exam);
        Integer benCiPaiming = 0;
        SortScore sortScore = null;
        List<SortScore> benciPaiming = new ArrayList<SortScore>();
        if (s.size() > 0) {
            for (ExamStudent examStudent : s) {
                sortScore = new SortScore();
                sortScore.paiming = benCiPaiming++;
                sortScore.studentNick = examStudent.getStudentNick();
                benciPaiming.add(sortScore);
            }
        }

        List<ExamStudent> s2 = examStudentRepository.findByExamOrderByStuTotalScoreDescCreatedDateAsc(examLast);
        Integer lastPaiming = 0;
        SortScore sortScorelast = null;
        List<SortScore> lastPaimingLst = new ArrayList<SortScore>();
        if (s2 != null && s2.size() > 0) {
            for (ExamStudent examStudent : s2) {
                sortScorelast = new SortScore();
                sortScorelast.paiming = lastPaiming++;
                sortScorelast.studentNick = examStudent.getStudentNick();
                lastPaimingLst.add(sortScorelast);
            }
        }

        PaiMing paiMing = null;
        List<PaiMing> paiMingg = new ArrayList<PaiMing>();
        for (SortScore sortScore2 : benciPaiming) {
            for (SortScore sortScore22 : lastPaimingLst) {
                if (sortScore2.studentNick.equals(sortScore22.studentNick)) {
                    if (sortScore2.paiming - sortScore22.paiming >= 0) {
                        paiMing = new PaiMing();
                        paiMing.studentNick = sortScore2.studentNick;
                        paiMing.paiming = sortScore2.paiming - sortScore22.paiming;
                        paiMingg.add(paiMing);
                    }
                   
                }
            }
        }
        Collections.sort(paiMingg, new Comparator<PaiMing>() {
            @Override
            public int compare(PaiMing o1, PaiMing o2) {
                return o1.paiming - o2.paiming;
            }
        });
        List<PaiMing> smark1 = new ArrayList<PaiMing>();
        if (paiMingg.size() > 3) {
            smark1 = paiMingg.subList(paiMingg.size() - 3,paiMingg.size());
        } else {
            smark1 = paiMingg;
        }

        // -----波动度较大-------//

        // 本次考试
        List<ExamStudent> studentBenCi = examStudentRepository.findByExamAndStuTotalScoreNot(exam, -1);
        // 上次考试
        List<ExamStudent> studentLast = examStudentRepository.findByExamAndStuTotalScoreNot(examLast, -1);

        List<Double> bodongDu = new ArrayList<Double>();
        // 波动度
        Double boDong = 0.0;
        Fluctuate fluctuate = null;
        List<Fluctuate> fluctuateLst = new ArrayList<Fluctuate>();
        if (studentBenCi != null && studentBenCi.size() > 0 && studentLast != null && studentLast.size() > 0) {
            for (ExamStudent examStudentbenci : studentBenCi) {
                for (ExamStudent examStudentlast : studentLast) {
                    if (examStudentbenci.getStudentId().equals(examStudentlast.getStudentId())) {
                        bodongDu.add(examStudentbenci.getStuTotalScore());
                        bodongDu.add(examStudentlast.getStuTotalScore());
                        boDong = ReportAlgorithm.volatilityValue(bodongDu);
                        fluctuate = new Fluctuate();
                        fluctuate.bodongdu = boDong; // 波动度
                        fluctuate.studentNick = examStudentbenci.getStudentNick();// 名称
                        fluctuateLst.add(fluctuate);
                    }
                }
            }
        }

        /*
         * Collections.sort(fluctuateLst, new Comparator<Fluctuate>() {
         * 
         * @Override public int compare(Fluctuate o1, Fluctuate o2) { return
         * (int)(o2.bodongdu - o1.bodongdu); } });
         */
        for (i = 0; i < fluctuateLst.size(); i++) {
            for (j = i + 1; j < fluctuateLst.size(); j++) {
                if (fluctuateLst.get(i).bodongdu < fluctuateLst.get(j).bodongdu) {
                    Fluctuate fluctuate1 = null;
                    fluctuate1 = fluctuateLst.get(i);
                    fluctuateLst.set(i, fluctuateLst.get(j));
                    fluctuateLst.set(j, fluctuate1);
                }
            }
        }
        if (fluctuateLst.size() > 2) {
            fluctuateLst = fluctuateLst.subList(0, 2);
        }
        List<PaiMing> smark2 = new ArrayList<PaiMing>();
        if (fluctuateLst.size() > 0) {
            for (Fluctuate fluctuate2 : fluctuateLst) {
                for (PaiMing paiMing2 : paiMingg) {
                    if (fluctuate2.studentNick.equals(paiMing2.studentNick)) {
                        smark2.add(paiMing2);
                    }
                }
            }
        }
        /*
         * smark2.add(chazhiLst.get(0).studentNick + "退步"+chazhiLst.get(1)+"名");
         * smark2.add(chazhiLst.get(chazhiLst.size()-1).studentNick);
         */
        List<Double> studentBenCi2 = new ArrayList<Double>();
        if (studentBenCi != null && studentBenCi.size() > 0) {
            for (ExamStudent double1 : studentBenCi) {
                studentBenCi2.add(double1.getStuTotalScore());
            }
        }

        List<Double> studentLast2 = new ArrayList<Double>();
        if (studentLast != null && studentLast.size() > 0) {
            for (ExamStudent double1 : studentLast) {
                studentLast2.add(double1.getStuTotalScore());
            }
        }

        String smartk4 = "";
        double pianyiBenCi = 0.0;
        if (studentBenCi2.size() > 0) {
            pianyiBenCi = examBenCi.skValue(studentBenCi2);

        }
        double pianyiLast = 0.0;
        if (studentLast2.size() > 0) {
            pianyiLast = examLastModel.skValue(studentLast2);
        }
        if (pianyiBenCi < pianyiLast) {
            smartk4 += "通过老师努力学生成绩在进步";
        } else if (pianyiBenCi > pianyiLast) {
            smartk4 += "老师应加强学生薄弱点的巩固";
        } else {
            smartk4 += "两次考试成绩稳定,继续保持";
        }

        model.examBenCi = examBenCi;
        model.examLast = examLastModel;
        model.smark = smark;
        model.smark1 = smark1;
        model.smark2 = smark2; // 在前端判断如果排名差值小于0的话就是退步,大于0的话就是进步
        model.smark3 = smark3;
        model.smartk4 = smartk4;
        model.isLast = isLast;
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 本次考试详情信息
        public ExamBenCi examBenCi;
        // 上次考试详情信息
        public ExamLast examLast;
        // 两次考试最低分名单
        public List<String> smark;
        // 进步较大的同学
        public List<PaiMing> smark1;
        // 在前端判断如果排名差值小于0的话就是退步,大于0的话就是进步
        public List<PaiMing> smark2;
        // 第一句话
        public String smark3;
        // 最后一句话
        public String smartk4;
        //标记是否有上次考试 0: 有上次考试   1:没有上次考试
        public Integer isLast;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 考试id
        public String examId;
    }

    protected class SortScore {
        // 学生名称
        public String studentNick;
        // 排名
        public Integer paiming;
    }

    protected class PaiMing {
        // 学生名称
        public String studentNick;
        // 排名差值
        public Integer paiming;
    }

    protected class ExamBenCi {
        // 考试总分
        public Integer totalScore;
        // 本次考试name
        public String examName;
        // 本次考试试题数量
        public Integer questionCount;
        // 本次考试覆盖章节
        public List<String> charpterList;
        // 考试的参加人数
        public Long personCount;
        // 平均分
        public Double average;
        // 最高分
        public Double max;
        // 最低分
        public Double min;
        // 失分率
        public Integer lostScoreRate;
        // 不及格
        public Long bujigeCount;
        // 及格
        public Long jigeCount;
        // 良好
        public Long lianghaoCount;
        // 优秀
        public Long youxiuCount;
        // 知识点
        public List<String> knowledgeList;
        // 不及格分数
        public Integer bujigeScore;
        // 及格分数
        public Integer jigeScore;
        // 良好分数
        public Integer lianghaoScore;
        // 优秀分数
        public Integer youxiuScore;
        // 不及格人数所占的百分比
        public Double bujigePerson;
        // 及格人数所占的百分比
        public Double jigePerson;
        // 良好人数所占的百分比
        public Double lianghaoPerson;
        // 优秀人数所占的百分比
        public Double youxiuPerson;

        public double skValue(List<Double> scoreLst) {
            Skewness skewness = new Skewness();
            int size = scoreLst.size();
            double[] arr = new double[size];
            for (int i = 0; i < scoreLst.size(); i++) {
                arr[i] = scoreLst.get(i);
            }
            double getSk = skewness.evaluate(arr);
            return getSk;
        }
    }

    protected class ExamLast {
        // 上次考试总分
        public Integer totalScore;
        // 上次考试name
        public String examName;
        // 上次考试试题数量
        public Integer questionCount;
        // 上次考试覆盖章节
        public List<String> charpterList;
        // 上次考试的参加人数
        public Long personCount;
        // 上次平均分
        public Double average;
        // 上次最高分
        public Double max;
        // 上次最低分
        public Double min;
        // 上次失分率
        public Integer lostScoreRate;
        // 上次不及格
        public Long bujigeCount;
        // 上次及格
        public Long jigeCount;
        // 上次良好
        public Long lianghaoCount;
        // 上次优秀
        public Long youxiuCount;
        // 上次知识点
        public List<String> knowledgeList;
        // 不及格分数
        public Integer bujigeScore;
        // 及格分数
        public Integer jigeScore;
        // 良好分数
        public Integer lianghaoScore;
        // 优秀分数
        public Integer youxiuScore;
        // 不及格人数所占的百分比
        public Double bujigePerson;
        // 及格人数所占的百分比
        public Double jigePerson;
        // 良好人数所占的百分比
        public Double lianghaoPerson;
        // 优秀人数所占的百分比
        public Double youxiuPerson;

        public double skValue(List<Double> scoreLst) {
            Skewness skewness = new Skewness();
            int size = scoreLst.size();
            double[] arr = new double[size];
            for (int i = 0; i < scoreLst.size(); i++) {
                arr[i] = scoreLst.get(i);
            }
            double getSk = skewness.evaluate(arr);
            return getSk;
        }
    }

    protected class Fluctuate {
        // 学生的波动度
        public Double bodongdu;
        // 学生的名称
        public String studentNick;
    }

    protected class ScoreFenBu {
        public String key;
        public Double value;
    }

}
